package com.example.bookmanager;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.xuyang.other.Flags;
import com.xuyang.other.ProgressDialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class BookManageActivity extends Activity {
	private ImageView mImgFinish;
	private TextView mTxtTitle;
	private EditText mEditBookId;
	private Button mBtnSubmit;
	
	private RequestQueue mQueue;
	private AlertDialog mDialog;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_book_manage);
		final int flag=getIntent().getFlags();
		init();
		if(flag==2){
			mTxtTitle.setText("归还图书");
		}else {
			mTxtTitle.setText("挂失图书");
		}
		mImgFinish.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		mBtnSubmit.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				mDialog.show();
				String url=Flags.BOOK_MANAGE.replace("FLAG", String.valueOf(flag)).replace("ID", mEditBookId.getText().toString());
				StringRequest request=new StringRequest(url, new Response.Listener<String>() {
					@Override
					public void onResponse(String str) {
						mDialog.dismiss();
						if (str.equals("ok")) {
							Toast.makeText(getBaseContext(), "操作成功", 0).show();
						}else {
							Toast.makeText(getBaseContext(), "操作失败", 0).show();
						}
					}
				}, new Response.ErrorListener() {
					@Override
					public void onErrorResponse(VolleyError arg0) {
						mDialog.dismiss();
						Toast.makeText(getBaseContext(), "请检查网络连接", 0).show();
					}
				});
				mQueue.add(request);
			}
		});
	}
	private void init() {
		mImgFinish=(ImageView) findViewById(R.id.img_bookmanage_finish);
		mTxtTitle=(TextView) findViewById(R.id.txt_bookmanage_title);
		mEditBookId=(EditText) findViewById(R.id.edit_bookmanage_id);
		mBtnSubmit=(Button) findViewById(R.id.btn_bookmanage_submit);
		mQueue=Volley.newRequestQueue(this);
		mDialog=new ProgressDialog(this).getDialog();
		mDialog.dismiss();
	}

}
